package com.laolang.shop.modules.im.request.util;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.laolang.shop.modules.im.consts.ImStatusCodeConst;
import com.laolang.shop.modules.im.consts.ImUrlConst;
import com.laolang.shop.modules.im.request.pojo.base.ImBaseResponse;
import com.laolang.shop.modules.im.request.pojo.imopenloginsvc.ImOpenLoginSvcAccountImportRequest;
import com.laolang.shop.modules.im.util.ImUrlGen;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * khl 修改为 resttmplate okhttp
 *
 * @author khlbat
 * @version 1.0
 * @date 2020/11/2 22:14
 */
@AllArgsConstructor
@Component
public class ImRequest {

    private final ImUrlGen imUrlGen;

    public ImBaseResponse imOpenLoginSvcAccountImport(ImOpenLoginSvcAccountImportRequest request) {
        ObjectMapper objectMapper = new ObjectMapper();
        String url = imUrlGen.genUrl(ImUrlConst.ImOpenLoginSvc.ACCOUNT_IMPORT);
        try {
            HttpResponse response = HttpUtil.createPost(url).body(objectMapper.writeValueAsString(request)).execute();
            if (response.isOk()) {
                ImBaseResponse result = objectMapper.readValue(response.body(), ImBaseResponse.class);
                if (ImStatusCodeConst.isOk(result.getActionStatus())) {
                    return result;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ImBaseResponse.buildFall();
    }
}
